<?php
namespace app\home\controller;
/*
文章管理
*/

use app\home\model\FreightModel;
use app\home\model\MessageModel;
use app\home\model\CommonModel;
use app\home\Controller\Commtype;
use think\Controller;
use think\Request;
use think\Cache;
use think\Db;


class Message extends Controller
{
    public function __construct(Request $request = null)
    {
        $result = $this->_getBid();
        if(!$result){
            $this->success('未登录', 'index/login');
        }
    }

    /**
     * 首页展示
     * @return \think\response\View
     *
     */
    public function index(){
        $bid = $this->_getBid();
        $return = MessageModel::instance()->show($bid);
        $data = $return[1];$page = $return[0];
        return view('message/index',['data'=>$data,'page'=>$page]);
    }
    /**
     * 编辑
     * @return \think\response\View
     *
     */
    public function edit(Request $request)
    {
        $id = $request->get('id');
        $bid = $this->_getBid();
        $details = Db::name('article')->find($id);
        $menus = MessageModel::instance()->menus($bid);
        // 获取二级分类
        $cats[0] = MessageModel::instance()->menus($bid);
        $cats[1] = Db::name('comm_type')->field('ptid,name')->where(['status'=>1,'level'=>0,'bid'=>$bid,'tid'=>$details['cat'],'type'=>0])->select();
        $cats[2] = Db::name('comm_type')->field('ptid,name')->where(['status'=>1,'pid'=>$details['cat2'],'type'=>0])->select();
        foreach ($cats[0] as $k => $v) {
            if ($v['tid'] == $details['cat']) {
                $details['menu1Name'] = $v['name'];
            }
        }
        foreach ($cats[1] as $k => $v) {
            if ($v['ptid'] == $details['cat2']) {
                $details['menu2Name'] = $v['name'];
            }
        }
        foreach ($cats[2] as $k => $v) {
            if ($v['ptid'] == $details['cat3']) {
                $details['menu3Name'] = $v['name'];
            }
        }
        if (!isset($details['menu3Name'])) {
            $details['menu3Name'] = '不存在';
        }
        return view('message/edit',['menus'=>$menus,'data'=>$details,'cats'=>$cats]);

    }
    /**
     * 详情
     */
    public function show(Request $request){
        $id = $request->get('id');
        $details = MessageModel::instance()->details($id);
        $details['pics'] ? $details['pics'] = explode(',',$details['pics']) : $details['pics'] = [];
        $details['pic'] = uploadpath('goods',$details['pic']);
        $details['freight_count'] == 0 ? $details['freight_count'] = '重量' : $details['freight_count'] = '件数';
        $details['shelves_time'] = date('Y-m-d H:i:s',$details['shelves_time']);;
        $details['createtime'] = date('Y-m-d H:i:s',$details['createtime']);;
        $details['updatetime'] == 0 ? $details['updatetime'] = '' : $details['freight_count'] = date('Y-m-d H:i:s',$details['freight_count']);
        foreach ($details['pics'] as &$val){
            $val = uploadpath('goods',$val);
        }
        return view('goods/show',['data'=>$details]);
    }


    /**
     * 商品添加
     * @return \think\response\View
     *
     */
    public function add()
    {
        $bid = $this->_getBid();
        $menus = MessageModel::instance()->menus($bid);
        return view('message/add',['menus'=>$menus]);
    }
    /**
     * 商品添加
     * @return \think\response\View
     *
     */
    public function templateajax()
    {

        $bida = [
            'bid' => $this->_getBid(),
            'status' => 1,
            'type' =>1
        ];

        $freight = MessageModel::instance()->freight($bida);

        return $freight;
    }
    

    /**
     * 添加数据库
     * @    param Request $request
     */
    public function insert(Request $request){
        $video = request()->file('video');
        header('Content-type:text/html;charset=utf-8');
        $data = $request->post();
        if (!isset($data['content'])) {
            $this->error('请填写内容');
        }
        if ($video){
            $video = $this->upVideo($video);
            $sqldata['video'] = $video;
        }
        $sqldata['name'] = $data['name'];
        $sqldata['cat'] = $data['datatype_one'];
        $sqldata['cat2'] = $data['menu2'];
        $sqldata['cat3'] = $data['menu3'];
        $sqldata['content'] = $data['content'];
        $result = Db::name('article')->insert($sqldata);
        if($result > 0){
            $this->success('添加成功', 'message/index');
        }else{
            $this->error('新增失败');
        }
        
    }
    /**
     * 修改数据库
     * @    param Request $request
     */
    public function editinsert(Request $request){
        $video = request()->file('video');

        header('Content-type:text/html;charset=utf-8');
        $data = $request->post();
        if (!isset($data['content'])) {
            $this->error('请填写内容');
        }
        if ($video){
            $video = $this->upVideo($video);
            $sqldata['video'] = $video;
        }
        $sqldata['name'] = $data['name'];
        $sqldata['cat'] = $data['datatype_one'];
        $sqldata['cat2'] = $data['menu2'];
        $sqldata['cat3'] = $data['menu3'];
        $sqldata['content'] = $data['content'];
        $res = Db::name('article')->where('id',$data['id'])->update($sqldata);
        if ($res) {
            $this->success('修改成功','message/index');
        }else{
            $this->success('修改失败或未修改');
        }
    }
    /*
    *删除
    */
    public function del(Request $request){
        $id  = $request->get('id');
        $res = Db::name('article')->where('id',$id)->update(['del'=>1]);
        if ($res) {
            $this->success('删除成功');
         } else{
            $this->error('删除失败');
         }
    }
    /**
     * 搜索
     */
    public function articleSearch(Request $request){
        $data = $request->get();
        $bid = $this->_getBid();
        $return = MessageModel::instance()->articleSearch($data,$bid);
        $data = $return[1];$page = $return[0];
        return view('message/index',['data'=>$data,'page'=>$page]);
    }
    /*
     * 改排序
     */
    public function editSort(Request $request){
        $data = $request->get();
        $bid = $this->_getBid();
    //向上
        if($data['index'] == 1){
            $sort = db('goods')->where('bid',8)->where('goodsid',$data['id'])->field('sort')->find();
            $arr = [
                'sort' =>$sort['sort']+1
            ];
            $add = db('goods')->where('bid',8)->where('goodsid',$data['id'])->update($arr);
            if($add){
                echo "<script>history.go(-1)</script>";
            }
        }
        //向下
        if($data['index'] == 2){
            $sort = db('goods')->where('bid',8)->where('goodsid',$data['id'])->field('sort')->find();
            $arr = [
                'sort' =>$sort['sort'] - 1
            ];
            $add = db('goods')->where('bid',8)->where('goodsid',$data['id'])->update($arr);
            if($add){
                echo "<script>history.go(-1)</script>";
            }
            //  var_dump($add);
        }
        //置顶
        if($data['index'] == 3){
            $sort = db('goods')->where('bid',8)->max('sort');
            $arr = [
                'sort' =>$sort + 1
            ];
            $add = db('goods')->where('bid',8)->where('goodsid',$data['id'])->update($arr);
            if($add){
                echo "<script>history.go(-1)</script>";
            }
        }
        if($data['index'] == 4){
//            $sort = db('goods')->where('bid',8)->min('sort');
            $arr = [
                'sort' =>0
            ];
            $add = db('goods')->where('bid',8)->where('goodsid',$data['id'])->update($arr);
            if($add){
                echo "<script>history.go(-1)</script>";
            }
        }
    }
    /*
    *视频上传
    */
    public function upVideo($video){
        // 获取表单上传文件
        $file = $video;
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS .'uploads'. DS . 'video',randCode(6));
        if($info){
            // 成功上传后 获取上传信息
            return  $info->getSaveName();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }

    }
}
